﻿package bdz.projects.lavender.doi.ui
{
	import bdz.common.managers.AudioControlManager;
	import bdz.common.managers.NetStreamManager;
	import bdz.common.events.*;
	import bdz.projects.lavender.doi.events.DOIEvent;
	import bdz.projects.lavender.doi.model.DOIShellReader;
	import bdz.projects.lavender.doi.ui.core.DOICoreView;
	import bdz.projects.lavender.doi.utils.CuePointManager;
	import flash.events.Event;
	import flash.media.Video;
	
	
	/**
	 * ...
	 * @author ...
	 */
	public class DOICategoryView extends DOICoreView 
	{
		private var _modlist:Array;
		private var vidStream:NetStreamManager;
		private var voiceStream:AudioControlManager;
		private var cpm:CuePointManager;
		private var reader:DOIShellReader = DOIShellReader.getReader(DOIShellReader.CONFIGURATION);
		function DOICategoryView() {
			super();
		}
		private function testMatch(evt:GenericEvent):void {
			trace("caption >> " + evt.data.caption + " at >> " + evt.data.markertime);
		}
		override protected function cleanUpComponent(evt:Event):void {
			
		}
		
		override protected function renderComponent():void {
			
			cpm = CuePointManager.getInstance();
			cpm.timeProperty = "rmarkertime";
			cpm.applyCompareList(convertCaptionToArray(new XML(dsource.definition.toString())));
			cpm.addEventListener(GenericEvent.MATCHED, testMatch);
			
			//load the modules under this category
			reader.getModules(dsource.id, onModulesLoaded);
			
			/*
			 * IF GOING TO USE AUDIO DELETE NETSTREAM MANAGER
			 * AND REPLACE WITH AUDIOCONTROL MANAGER
			 * 
			 * Use voiceStream variable
			 */
			vidStream = new NetStreamManager();
			vidStream.addEventListener(NetStreamManagerEvent.LOAD_PROGRESS, onStreamLoadProgress);
			vidStream.addEventListener(NetStreamManagerEvent.LOAD_COMPLETE, onStreamLoadComplete);
			vidStream.addEventListener(NetStreamManagerEvent.PLAY_PROGRESS, onStreamPlayProgress);
			vidStream.addEventListener(NetStreamManagerEvent.PLAY_COMPLETE, onStreamPlayComplete);
			vidStream.addEventListener(NetStreamManagerEvent.META_DATA, onStreamMetaData);
			vidStream.source = dsource.swfimage;
			
			/**
			 * VOICE STREAM IMPLEMENTATION 
			 * (ALTERNATIVE TO VIDEO)
			 */
			voiceStream = new AudioControlManager();
			voiceStream.addEventListener(GenericEvent.LOAD_PROGRESS, onAudioLoadProgress);
			voiceStream.addEventListener(GenericEvent.COMPLETE, onAudioLoadComplete);
			voiceStream.addEventListener(GenericEvent.PROGRESS, onAudioStreamProgress);
		}
		protected function convertCaptionToArray(def:XML):Array {
			var retarr:Array = new Array();
			var xlist:XMLList = def.caption;
			for each(var xm:XML in xlist) {
				retarr.push(xm);
			}
			return retarr;
		}
		protected function onModulesLoaded(arr:Array):void {
			_modlist = arr;
			dispatchEvent(new DOIEvent(DOIEvent.MODULE_LOADED, _modlist));
		}
		
		private function onStreamMetaData(evt:NetStreamManagerEvent):void {
			
		}
		private function onAudioLoadProgress(evt:GenericEvent):void {
			
		}
		private function onStreamLoadProgress(evt:NetStreamManagerEvent):void {
			
		}
		private function onAudioLoadComplete(evt:GenericEvent):void {
			voiceStream.play();
		}
		private function onStreamLoadComplete(evt:NetStreamManagerEvent):void {
			//vidStream.play();
		}
		private function onAudioStreamProgress(evt:GenericEvent):void {
			cpm.analyzeTime(evt.data.position);
		}
		private function onStreamPlayProgress(evt:NetStreamManagerEvent):void {
			cpm.analyzeTime(vidStream.stream.time * 1000);
		}
		
		private function onStreamPlayComplete(evt:NetStreamManagerEvent):void {
			trace("play complete");
		}
		public function set moduleList(ml:Array):void {
			_modlist = ml;
		}
		public function get moduleList():Array {
			return _modlist;
		}
	}
	
}